package person.aoao.library.controller;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import person.aoao.library.entity.BookList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.BookListService;

import javax.annotation.Resource;

/**
 * (BookList)表控制层
 *
 * @author makejava
 * @since 2023-05-05 10:15:41
 */
@RestController
@CrossOrigin
@RequestMapping("bookList")
public class  BookListController {
    /**
     * 服务对象
     */
    @Resource
    private BookListService bookListService;

    /**
     * 分页查询
     *
     * @param bookList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<BookList>> queryByPage(BookList bookList, PageRequest pageRequest) {
        return ResponseEntity.ok(this.bookListService.queryByPage(bookList, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<BookList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.bookListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param bookList 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<BookList> add(BookList bookList) {
        return ResponseEntity.ok(this.bookListService.insert(bookList));
    }

    /**
     * 编辑数据
     *
     * @param bookList 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<BookList> edit(BookList bookList) {
        return ResponseEntity.ok(this.bookListService.update(bookList));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.bookListService.deleteById(id));
    }


    @GetMapping("getBookList/{num}/{page}")
    public ResponseData getBookList(@PathVariable Integer num, @PathVariable Integer page){
        ResponseData data=this.bookListService.getBookList(num,page);
        return data;
    }

    @PostMapping("searchBookList/{type}/{num}/{page}")
    public ResponseData searchBookList(@RequestBody String keyword, @PathVariable Integer type, @PathVariable Integer num, @PathVariable Integer page){
        System.out.println("+"+keyword);
        ResponseData data=this.bookListService.searchBookList(keyword.replace("\"", ""),type,num,page);
        return data;
    }

    @PostMapping("addBook")
    public ResponseData addBook(@RequestBody BookList bookList){
        return  new ResponseData(ResponseCode.SUCCESS,this.bookListService.insert(bookList));
    }

    @PostMapping("updateBook")
    public ResponseData updateBook(@RequestBody BookList bookList){
        return  new ResponseData(ResponseCode.SUCCESS,this.bookListService.update(bookList));
    }
    @PostMapping("deleteBook")
    public ResponseData deleteBook(@RequestBody BookList bookList){
        return  new ResponseData(ResponseCode.SUCCESS,this.bookListService.deleteById(bookList.getBookid()));
    }
}

